<?php

namespace App\Kuafu\CouponPackage\Controller\Merchant;

use App\Kuafu\Base\Controller\BaseController;
use App\Kuafu\Order\Enum\OrderMerchantEnum;
use App\Kuafu\Order\Service\OrderMerchantUseService;
use Illuminate\Http\Request;

class CouponPackageController extends BaseController
{
    //会员列表
    public function lists(Request $request)
    {
        $showTimeTab = (int)$request->get('showTimeTab', OrderMerchantEnum::SHOW_TIME_TAB_TODAY);
        $showStartAt = (int)$request->get('showStartAt', 0);
        $showEndAt   = (int)$request->get('showEndAt', 0);
        $nameIds     = (string)$request->get('nameIds');
        $phone       = (int)$request->get('phone');
        $lastUnionId = (int)$request->get('lastUnionId', 0);
        $limit       = (int)$request->get('limit', 20);
        if (mb_strlen($nameIds)){
            $couponPackageIds = explode(',', $nameIds);
        }else {
            $couponPackageIds = [];
        }
        $data = (new OrderMerchantUseService([$this->shopId]))
            ->setShowTimeTab($showTimeTab, $showStartAt, $showEndAt)
            ->couponPackageLists($couponPackageIds, $phone, $lastUnionId, $limit);
        return $this->success(['lists' => $data]);
    }

    //统计
    public function listsStatistics(Request $request)
    {
        $showTimeTab = (int)$request->get('showTimeTab', OrderMerchantEnum::SHOW_TIME_TAB_TODAY);
        $showStartAt = (int)$request->get('showStartAt', 0);
        $showEndAt   = (int)$request->get('showEndAt', 0);
        $nameIds     = (string)$request->get('nameIds');
        $phone       = (int)$request->get('phone');
        if (mb_strlen($nameIds)){
            $couponPackageIds = explode(',', $nameIds);
        }else {
            $couponPackageIds = [];
        }
        $data = (new OrderMerchantUseService([$this->shopId]))
            ->setShowTimeTab($showTimeTab, $showStartAt, $showEndAt)
            ->couponPackageListsStatistics($couponPackageIds, $phone);
        return $this->success($data);
    }

    //筛选
    public function listsFilter(Request $request)
    {
        $data = (new OrderMerchantUseService([$this->shopId]))->couponPackageListsFilter();
        return $this->success($data);
    }

    //详情
    public function detail(Request $request)
    {
        $orderId = (int)$request->get('orderId');

        $data = (new OrderMerchantUseService([$this->shopId]))
            ->couponPackageDetail($orderId);
        return $this->success($data);
    }
}
